﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;


namespace WpfApplication1
{
    class QuestionMgr
    {
        public QuestionMgr(string strQues, string strAnws)
        {
            this.Question = strQues;
            this.Anwser = strAnws;
        }

        public string Question
        {
            get;
            set;
        }

        public string Anwser
        {
            get;
            set;
        }
    }

    class Excel
    {
        private ArrayList m_QuestionAnswer;
        public Excel()
        {
            m_QuestionAnswer = new ArrayList();
        }

        public ArrayList QuestionAnswer
        {   
            get { return m_QuestionAnswer; }
        }

        public bool Open(string strFileName)
        {
            string strConn;
            if (strFileName.ToLower().EndsWith("xls"))
            {
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                    strFileName + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
            }
            else
            {
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"
                    + strFileName + "';Extended Properties='Excel 12.0;HDR=YES'";
            }

            OleDbConnection oleConn = new OleDbConnection(strConn);
            String strSql = "SELECT * FROM  [Sheet1$]";//可更改Sheet名称，比如sheet2，等等
            OleDbCommand command = new OleDbCommand(strSql, oleConn);

            oleConn.Open();
            OleDbDataReader reader = command.ExecuteReader();

            ArrayList listQues = new ArrayList();

            while (reader.Read())
            {
                int iColCount = reader.FieldCount;
                if (iColCount >= 2)
                {
                    string strQ = reader[0].ToString();
                    string strA = reader[1].ToString();
                    QuestionMgr oneQues = new QuestionMgr(strQ, strA);
                    listQues.Add(oneQues);
                }
            }
            this.m_QuestionAnswer.Clear();
            this.m_QuestionAnswer.AddRange(listQues);
            reader.Close();
            oleConn.Close();
            return true;
        }
    }
}
